package com.zuimei.zuimei.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zuimei.zuimei.R;
import com.zuimei.zuimei.beans.CategoryItemBean;

import java.util.List;

/**
 * Created by 任聪 on 2017/2/4.
 */
public class CategoryListAdapter extends RecyclerView.Adapter<CategoryListAdapter.MyViewHolder3> {
    private CategoryAdapter.OnCategoryItemClicklistener onItemClickListener;
    private Context context;
    private List<CategoryItemBean> list;
    public CategoryListAdapter(Context context,List<CategoryItemBean> list){
        this.list=list;
        this.context=context;
    }
    public void setOnItemClickListener(CategoryAdapter.OnCategoryItemClicklistener listener){
        this.onItemClickListener=listener;
    }
    @Override
    public MyViewHolder3 onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder3(LayoutInflater.from(context).inflate(R.layout.category_listview_item,parent,false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder3 holder, int position) {
        holder.textView1.setText(list.get(position).getTitle());
        holder.textView2.setText(list.get(position).getSub_title());
        Glide.with(context.getApplicationContext())
                .load(list.get(position).getIcon_url())
                .into(holder.imageView);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    class MyViewHolder3 extends RecyclerView.ViewHolder {
        TextView textView1, textView2;
        ImageView imageView;

        public MyViewHolder3(final View itemView) {
            super(itemView);
            textView1 = (TextView) itemView.findViewById(R.id.tv_category_item1);
            textView2 = (TextView) itemView.findViewById(R.id.tv_category_item2);
            imageView = (ImageView) itemView.findViewById(R.id.iv_category_item);
            //item点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onClick(itemView, getAdapterPosition());
                    }
                }
            });


            //设置item的长按事件

            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onLongClick(itemView, getAdapterPosition());
                    }
                    return true;
                }
            });

        }
    }
}
